package com.yy.finvideoplayer.manager

import android.content.Context
import android.content.SharedPreferences
import android.os.Build
import android.util.Log
import androidx.core.content.edit
import com.yy.finvideoplayer.Util
import com.yy.finvideoplayer.media.AndroidMedia
import com.yy.finvideoplayer.media.BaseMedia
import com.yy.finvideoplayer.media.IMediaCallback
import com.yy.finvideoplayer.media.IMediaControl
import com.yy.finvideoplayer.renderview.IRenderView
import com.yy.finvideoplayer.renderview.SufaceRenderView
import com.yy.finvideoplayer.renderview.TextureRenderView
import com.yy.finvideoplayer.videoview.MediaVideoView
import java.lang.ref.WeakReference
import java.util.concurrent.ConcurrentHashMap

/**
 * @Description: ConfigManage
 * @Author: yiw89
 * @CreateDate: 2025/9/1 15:03
 * @Version: 1.0
 */
class ConfigManage private constructor(context: Context) {
    private val TAG = "ConfigManage"
    private val preferences: SharedPreferences = context.getSharedPreferences(
        "cfg_qsvideo",
        Context.MODE_PRIVATE
    )
    var decodeClassName: String? = preferences.getString(
        "decodeClassName",
        AndroidMedia::class.java.name
    )
        set(value) {
            field = value
            preferences.edit { putString("decodeClassName", value) }
        }

    private var renderViewType: Int = preferences.getInt(
        "renderViewType",
        2
    )
        set(value) {
            field = value
            preferences.edit { putInt("renderViewType", value) }
        }


    fun getIRenderView(context: Context?, type: Int = 0): IRenderView {
        val actualType = if (type != 0) type else renderViewType

        return when (actualType) {
            1 -> SufaceRenderView(context)
            2 -> TextureRenderView(context)
            else -> {
                Log.e(TAG, "renderViewType设置有误,默认使用SufaceRenderView")
                SufaceRenderView(context)
            }
        }
    }


    fun newMediaControl(
        iMediaCallback: IMediaCallback,
        className: Class<out BaseMedia> = AndroidMedia::class.java
    ): IMediaControl {
        return Util.newInstance<IMediaControl>(className.name, iMediaCallback)
            ?: AndroidMedia(iMediaCallback).also {
                Log.e(TAG, "newInstance error: ${className.name}, fallback to AndroidMedia")
            }
    }

    fun addVideoView(q: MediaVideoView?) {
        q?.also { videoView ->
            videos[System.identityHashCode(videoView)] = WeakReference(videoView)
        }
    }

    companion object {
        @Volatile
        private var instance: ConfigManage? = null

        private val videos = ConcurrentHashMap<Int, WeakReference<MediaVideoView>>()
        private val lock = Any()

        fun getInstance(context: Context): ConfigManage {
            return instance ?: synchronized(lock) {
                instance ?: ConfigManage(context.applicationContext).also { instance = it }
            }
        }

        fun releaseAll() {
            videos.values.forEach { it.get()?.release() }
            videos.clear()
        }

        fun releaseOther(target: MediaVideoView) {
            videos.values
                .mapNotNull { it.get() }
                .filter { it !== target }
                .forEach { it.release() }
        }
    }
}